﻿package org.catapult.service {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import org.catapult.event.XMLServiceEvent;
	
	/**
	 * ...
	 * @author Lam Do
	 */
	public class XMLService extends EventDispatcher{
		
		private var _urlLoader: URLLoader;
		
		public function XMLService() {
			
		}
		
		public function execute(url: String): void {
			this._urlLoader = new URLLoader();
			this._urlLoader.addEventListener(Event.COMPLETE, xmlCompleteHandler);
			this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlErrorHandler);
			this._urlLoader.load(new URLRequest(url));
		}
		
		private function xmlErrorHandler(e:IOErrorEvent):void {
			var serviceEvent: XMLServiceEvent = new XMLServiceEvent(XMLServiceEvent.XML_ERROR);			
			this.dispatchEvent(serviceEvent);
		}
		
		private function xmlCompleteHandler(e:Event):void {			
			var serviceEvent: XMLServiceEvent = new XMLServiceEvent(XMLServiceEvent.XML_COMPLETE);
			serviceEvent.xml = new XML(e.target.data);
			this.dispatchEvent(serviceEvent);
		}
	}

}